using UnityEngine;
using UnityEngine.Events;

namespace Utility.GameEvents.Scriptable {
    public class GameEventListener<T> : MonoBehaviour, Core.IGameEventListener<T>
    {
        [SerializeField, Utility.Attributes.NotNull] protected GameEvent<T> gameEvent;
        [SerializeField] protected UnityEvent<T> response;

        protected virtual void OnEnable() => gameEvent?.Register(this);
        protected virtual void OnDisable() => gameEvent?.Unregister(this);

        public void AddListener(UnityAction<T> action) => response.AddListener(action);

        public void RemoveListener(UnityAction<T> action) => response.RemoveListener(action);

        public void OnEventRaised(T value) => response?.Invoke(value);

        // 允许外部调用 Raise，便于自动绑定按钮
        public void Raise(T value) => gameEvent?.Raise(value);
    }
}